package Sort.sort2022;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2022-04-11
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] array = {3, 5, 8, 4, 1, 6, 2, 9, 7};
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }

    public static void sort(int[] array) {
        if (array == null) {
            return;
        }

        for (int i = array.length - 1; i > 0; i--) {
            for (int j = 0; j <= i - 1 ; j++) {
                if (array[j + 1] < array[j]) {
                    swap(array, j + 1, j);
                }
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
